"use strict";

const Service = require("egg").Service;

class ValidateService extends Service {
    /**
     * 验证邮箱格式
     * @param {string} email 邮箱
     * @throws 邮箱格式错误
     */
    async verifyEmail(email) {
        const pattern = /^[a-zA-Z0-9]+([-_.][a-zA-Z0-9]+)*@[a-zA-Z0-9]+([-_.][a-zA-Z0-9]+)*\.[a-z]{2,}$/;
        if (!email || !pattern.test(email)) {
            this.ctx.throw(400, "邮箱格式错误", { user: "user" });
        }
    }

    /**
     * 验证密码格式
     * @param {string} password 密码
     * @throws 密码格式错误
     */
    async verifyPassword(password) {
        if (!password) {
            this.ctx.throw(400, "密码格式错误", { user: "user" });
        }
    }
}

module.exports = ValidateService;
